package Accessors;

import java.io.IOException;
import java.util.ArrayList;

import utils.RESTfulConnect;
import utils.URLTools;
import Callbacks.AbstractCallback;
import Enums.RFAT;
import Models.Keg;
import Models.KegList;
import Presenter.KegListPresenter;
import Presenter.KegPresenter;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

@SuppressWarnings("rawtypes")
public class KegAccessor extends AbstractAccessor<Keg,KegPresenter,AbstractCallback> {
	private final String NAME = "scantblkeg";
	private final String DIALOG = "Keg(s)";
	
	private final static KegPresenter presenter = new KegPresenter();
	
	AbstractCallback callback;
	
	//Modal dialog variable.
	ProgressDialog modalDialog = null;
	
	public KegAccessor(AbstractCallback callback){
		super(presenter, callback);
		this.callback = callback;
	}
	
	//Public access functions.
	public void getAllItems(final Context context){
		String dialog = "Loading " + DIALOG + " List...";
		String rfString = NAME;
		super.getAllItems(context, dialog, rfString);
	}
	
	public void getItem(final int id, final Context context){
		String dialog = "Loading " + DIALOG + "...";
		String rfString = NAME + "/" + Integer.toString(id);
		super.getItem(context, dialog, rfString);
	}
	
	public void doItemUpdate(final Keg item, final Context context){
		String dialog = "Updating " + DIALOG + "...";
		String rfString = NAME;
		super.doItemUpdate(item, context, dialog, rfString);
	}
	
	public void doItemInsert(final Keg item, final Context context){
		String dialog = "Inserting " + DIALOG + "...";
		String rfString = NAME;
		super.doItemInsert(item, context, dialog, rfString);
	}
	
	
	//Non standardized code
	public void doKegSplit(final Keg keg, final KegList kegList, final Context context){
		modalDialog = ProgressDialog.show(context, null, "Processing Keg List Update...");
		final String strStart = URLTools.getURLString(context);
		final String str = NAME + "/processKegSplitAndroid/" + Integer.toString(keg.getKegID());
		KegListPresenter presenter = new KegListPresenter();
		new UpdateKegListTask().execute(strStart+str, URLTools.getUSERString(context), URLTools.getPASSString(context), presenter.createJSON(kegList).toString());
	}
	
	private class UpdateKegListTask extends AsyncTask<String, Void, String> {
    	@Override
        protected String doInBackground(String... urls) {
            try {
                return RESTfulConnect.postResource(urls[0], urls[1], urls[2], urls[3]);
            } catch (IOException e) {
            	e.printStackTrace();
            	return "Connection Error";
            }
        }
    	@SuppressWarnings("unchecked")
		@Override
        protected void onPostExecute(String result) {
    		ArrayList<Keg> items = new ArrayList<Keg>();
    		Keg item = new Keg();
    		modalDialog.dismiss();
    		callback.callback(item, RFAT.UPDATE_KEG_LIST, items);
        }
    }
	
	//Non standardized code
	public void doBCSToKeg(final KegList kegList, final int bcsid, final Context context){
		modalDialog = ProgressDialog.show(context, null, "Packageing To Kegs...");
		final String strStart = URLTools.getURLString(context);
		final String str = NAME + "/processScannedBarrelAgedKegFillAndroid/" + Integer.toString(bcsid);
		KegListPresenter presenter = new KegListPresenter();
		new UpdateKegListTask().execute(strStart+str, URLTools.getUSERString(context), URLTools.getPASSString(context), presenter.createJSON(kegList).toString());
	}
	
	//Non standardized code
	public void doKegListDestroyal(final KegList kegList, final String notes, final Context context){
		modalDialog = ProgressDialog.show(context, null, "Packageing To Kegs...");
		final String strStart = URLTools.getURLString(context);
		String str = NAME + "/processKegLossesAndroid/" + notes;
		str = str.replaceAll(" ", "%20");
		KegListPresenter presenter = new KegListPresenter();
		new UpdateKegListTask().execute(strStart+str, URLTools.getUSERString(context), URLTools.getPASSString(context), presenter.createJSON(kegList).toString());
	}
}
